package main;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import main.*;
import dao.*;


public class Reporter {
  
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    JTable table = new JTable(new MyTableModel());
    table.setAutoCreateRowSorter(true);  
    
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }

  
  static class MyTableModel extends AbstractTableModel {
    int rowCount;
    String[] columnNames = {"Username",
                                        "Country",
                                        "City",
                                        "Algorithm",
                                        "Date"};
    Object[][] data;
    
    
    public MyTableModel(){
    	
        
    	AccessesDAOImpl dao = new AccessesDAOImpl();
        List<DataTransferObject> dbList = dao.getAllData();                               
        //List dbList = new ArrayList(3);
        this.rowCount = dbList.size();
       
        this.data = new Object[rowCount][5];
        Calendar cl = Calendar.getInstance();
        for(int i = 0; i < rowCount; i++){
        	Vector row = new Vector(5);
            row.add(dbList.get(i).getUserName());
            row.add(dbList.get(i).getCountry());
            row.add(dbList.get(i).getCity());
            row.add(dbList.get(i).getAlgorithmName());
            row.add((dbList.get(i).getTime().getTime().toString()));
            
            //row.add(cl);
            for(int j = 0; j<5;j++){
             data[i][j] = row.get(j);
            }
        }
        
    }
       
        public int getColumnCount() {
            return 5;
        }

        public int getRowCount() {
            return rowCount;
        }
        public boolean isCellEditable(int row, int col){
            return false;
        }
        public Class getColumnClass(int column) {
        	return (getValueAt(0,column).getClass());
        	}
        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

     
    } 

}